package com.ruoyi.whole.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.whole.domain.DmsDealerVehicleInfo;
import com.ruoyi.whole.mapper.DmsDealerVehicleInfoMapper;
import com.ruoyi.whole.service.IDmsDealerVehicleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 车辆库存信息Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-03
 */
@Service
public class DmsDealerVehicleInfoServiceImpl extends ServiceImpl<DmsDealerVehicleInfoMapper,DmsDealerVehicleInfo> implements IDmsDealerVehicleInfoService
{
    @Autowired
    private DmsDealerVehicleInfoMapper dmsDealerVehicleInfoMapper;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private AutofillUtil autofillUtil;

    /**
     * 查询车辆库存信息
     * 
     * @param id 车辆库存信息主键
     * @return 车辆库存信息
     */
    @Override
    public DmsDealerVehicleInfo selectDmsDealerVehicleInfoById(Long id)
    {
        return dmsDealerVehicleInfoMapper.selectDmsDealerVehicleInfoById(id);
//        return lambdaQuery().eq(DmsDealerVehicleInfo::getId,id).one();
    }

    /**
     * 查询车辆库存信息列表
     * 
     * @param dmsDealerVehicleInfo 车辆库存信息
     * @return 车辆库存信息
     */
    @Override
    public List<DmsDealerVehicleInfo> selectDmsDealerVehicleInfoList(DmsDealerVehicleInfo dmsDealerVehicleInfo)
    {
        return dmsDealerVehicleInfoMapper.selectDmsDealerVehicleInfoList(dmsDealerVehicleInfo);
    }

    /**
     * 新增车辆库存信息
     * 
     * @param dmsDealerVehicleInfo 车辆库存信息
     * @return 结果
     */
    @Override
    public int insertDmsDealerVehicleInfo(DmsDealerVehicleInfo dmsDealerVehicleInfo)
    {
//        return dmsDealerVehicleInfoMapper.insertDmsDealerVehicleInfo(dmsDealerVehicleInfo);

//        //1. 判断是店端账号还是厂端账号，厂端可以看两端库存，店端只能看店端的库存
//        //①根据部门id找到父部门id，判断是经销商还是厂端（经销商是101 102，厂端201）
//        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
//        SysDept sysDept = iSysDeptService.selectDeptById(deptId);
//        if (sysDept.getParentId().equals(101L) || sysDept.getParentId().equals(102L) ){
//            dmsDealerVehicleInfo.setInventoryType("店端");
//        }
//        else {
//            dmsDealerVehicleInfo.setInventoryType("厂端");
//        }

        //限制创建的状态只能是厂端在库
        if (!dmsDealerVehicleInfo.getVehicleStatus().equals(0)){
            throw new ServiceException("新建的车辆状态只能是 厂端在库！");
        }
        //限制输入的价格必须是大于0
        if (dmsDealerVehicleInfo.getPrice()<=0){
            throw new ServiceException("车辆价格必须大于0");
        }
        //没有车架号就自动生成
        if (StrUtil.isBlank(dmsDealerVehicleInfo.getVin())){
            dmsDealerVehicleInfo.setVin(IdUtils.fastSimpleUUID());
        }
        //没有变速箱号，后台自动生成
        if (StrUtil.isBlank(dmsDealerVehicleInfo.getTransmissionModel())){
            dmsDealerVehicleInfo.setTransmissionModel(IdUtils.fastSimpleUUID());
        }
        //刚开始创建都是厂端，只有采购订单签收才是店端
        dmsDealerVehicleInfo.setInventoryType("厂端");
        boolean save = save(dmsDealerVehicleInfo);
        return 1;
    }

    /**
     * 修改车辆库存信息
     * 
     * @param dmsDealerVehicleInfo 车辆库存信息
     * @return 结果
     */
    @Override
    public int updateDmsDealerVehicleInfo(DmsDealerVehicleInfo dmsDealerVehicleInfo)
    {
        //修改自动填充
        autofillUtil.autoFillUpdate(dmsDealerVehicleInfo);
        //添加审核人、审核时间、审核人id
        dmsDealerVehicleInfo.setAuditorId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsDealerVehicleInfo.setAuditorName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsDealerVehicleInfo.setAuditorTime(new Date());
        return dmsDealerVehicleInfoMapper.updateDmsDealerVehicleInfo(dmsDealerVehicleInfo);
    }

    /**
     * 批量删除车辆库存信息
     * 
     * @param ids 需要删除的车辆库存信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsDealerVehicleInfoByIds(Long[] ids)
    {
        return dmsDealerVehicleInfoMapper.deleteDmsDealerVehicleInfoByIds(ids);
    }

    /**
     * 删除车辆库存信息信息
     * 
     * @param id 车辆库存信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsDealerVehicleInfoById(Long id)
    {
        return dmsDealerVehicleInfoMapper.deleteDmsDealerVehicleInfoById(id);
    }
}
